package models;
import java.text.Format;
import java.text.ParseException;
import java.util.Date;
import javax.persistence.*;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.Page;
import play.data.format.Formats;
import play.data.validation.Constraints;
import play.db.ebean.Model;
import controllers.JsonDateFormatter;
import controllers.JsonDatetimeFormatter;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import controllers.Helper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import play.libs.Json;
import models.Vehicle;
import models.Driver;
import models.Staff;
@Entity
@Table(name="safety_check")
public class Safety_check extends Model {
	private static final long serialVersionUID = 1L;
	@Id
	public Integer id; //编号
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date notice_date; //通知日期
	@ManyToOne
	public Vehicle vehicle; //车号
	@ManyToOne
	public Driver notice_who; //通知司机
	@ManyToOne
	public Staff checker; //检查监督人
	
	public String check_site; //检查地点
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date check_date; //检查日期
	
	public String check_type; //检查类型
	
	public String check_item; //检查项目
	
	public String check_conclusion; //检查结论
	
	public String action; //处理措施
	
	public String writer; //登记人
	public static Model.Finder<Integer, Safety_check> find = new Model.Finder<Integer, Safety_check>(Integer.class, Safety_check.class);

	public static Page<Safety_check> page(int page, int pageSize, String sortBy, String order, String [] filterColum, String[] filter) {
		ExpressionList<Safety_check> exp = find.where();
		for(int i= 0; i< filterColum.length; i++) {
			String[] tokens = filter[i].split("\\|", 3);
			if (tokens.length == 0) {
				continue;
			}
			String fieldtype = tokens[0];
			if (tokens.length == 3) { //two values, range search
				if (tokens[1].isEmpty() || tokens[2].isEmpty() ) {
					continue;
				}
				Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
				Object obj1 = Helper.stringToObject(fieldtype, tokens[2]);
				exp = exp.between(filterColum[i], obj0, obj1);
			} else if (tokens.length == 2) { //one value search
				if (tokens[1].isEmpty()) {
					continue;
				}
				if (fieldtype.equalsIgnoreCase("String")) {
					exp = exp.ilike(filterColum[i], "%" + tokens[1] + "%");
				} else {
					Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
					exp = exp.eq(filterColum[i], obj0);
				}
			}
		}
		return exp.orderBy(sortBy + " " + order).findPagingList(pageSize).setFetchAhead(false).getPage(page);
	}
	public void setId(Integer d) {
		id= d;
	}
	public Integer getId() {
		return id;
	}
	public void setNotice_date(Date d) {
		notice_date= d;
	}
	public Date getNotice_date() {
		return notice_date;
	}
	public void setVehicle(Vehicle d) {
		vehicle= d;
	}
	public Vehicle getVehicle() {
		return vehicle;
	}
	public void setNotice_who(Driver d) {
		notice_who= d;
	}
	public Driver getNotice_who() {
		return notice_who;
	}
	public void setChecker(Staff d) {
		checker= d;
	}
	public Staff getChecker() {
		return checker;
	}
	public void setCheck_site(String d) {
		check_site= d;
	}
	public String getCheck_site() {
		return check_site;
	}
	public void setCheck_date(Date d) {
		check_date= d;
	}
	public Date getCheck_date() {
		return check_date;
	}
	public void setCheck_type(String d) {
		check_type= d;
	}
	public String getCheck_type() {
		return check_type;
	}
	public void setCheck_item(String d) {
		check_item= d;
	}
	public String getCheck_item() {
		return check_item;
	}
	public void setCheck_conclusion(String d) {
		check_conclusion= d;
	}
	public String getCheck_conclusion() {
		return check_conclusion;
	}
	public void setAction(String d) {
		action= d;
	}
	public String getAction() {
		return action;
	}
	public void setWriter(String d) {
		writer= d;
	}
	public String getWriter() {
		return writer;
	}
	public static Safety_check findById(Integer d) {
		return find.byId(d);
	}
	public static String getIdJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Safety_check v: find.all()) {
			node.put(v.getId().toString(), v.getId().toString()); 
		}
		return node.toString();
	}
}
